import { world, Player } from "@minecraft/server";

export class LandList {
    /**
     * @type { String }
     */
    __namespace = "land_list"

    /**
     * 地皮数据
     * @type { {
     *  admin: String,
     *  area: {
     *      min: {
     *          x: Number,
     *          z: Number
     *      }, 
     *      max: {
     *          x: Number,
     *          z: Number
     *      }
     *  },
     *  index: Number
     * }[] }
     */
    __land_data_list = []


    /**
     * @type { Player }
     */
    __player


    constructor() {
    }


    get_all_plot_data() {
        const data = world.getDynamicProperty(this.__namespace);
        return data;
    }

    /**
     * 添加地皮数据
     * @param {Player} player
     * @param {{min: {x: Number;z: Number;};max: {x: Number; z: Number;};}} data
     * @param {Number} index
     */
    app_end_data_list(player, data, index) {
        const land_data = {
            admin: player.name,
            area: data,
            index: index
        }
        let player_repeat = false;

        this.__land_data_list = JSON.parse(this.get_all_plot_data() == "" ? "[]" : this.get_all_plot_data());

        for (const land_data_in_list of this.__land_data_list) {
            if (land_data.admin == land_data_in_list.admin) {
                player_repeat = true;
                break;
            }
        }

        if (player_repeat) return "玩家重复";

        this.__land_data_list.push(land_data);

        world.setDynamicProperty(this.__namespace, JSON.stringify(this.__land_data_list));
    }
}